/**
 * Created by Dalyn on 2017/5/19.
 */
import React from "react";
import {hashHistory} from 'react-router';
import Relay from "react-relay";
import InputBox from "../../../common/InputBox"
import Pagination from "../Pagination"
import { Modal,Table,message,Input } from 'antd';
import ReactKey from "../../../util/ReactKey";
import GetChildsGoodsMutation from '../mutation/fabricRawMutation/GetChildsGoodsMutation';
import "../css/fabricRaw.css";
import "../css/productionManagement_common.css";

export default class SelectMaterialModal extends React.Component{
    constructor(props) {
        super(props);
        this.state = {
           /* pageSize:"10",
            pageNum:"1",
            totalResult:"",*/
            orderId:"",
            SelectMaterialFlag:false,
            orderType:"3",
            loading:false,
            dataSource:[],
            list:[],
        };
        this.style = {

        };
        // this.list=[];
        // this.materialIndex = 0;
    }
    componentWillMount(){
        // this.getChildsGoods()
    }
    componentDidMount(){

    }
    componentWillReceiveProps(){
        this.setState({
            orderId:this.props.orderId,
        })
    }
    getChildsGoods(){
        Relay.Store.commitUpdate(
            new GetChildsGoodsMutation({
                pageSize: this.state.pageSize,
                pageNum: this.state.pageNum,
                orderId: this.state.orderId,
                orderType: this.state.orderType,
            }),
            {
                onFailure: () => {
                    message.error('网络请求失败');
                },
                onSuccess: (response) => {
                    let errorCode = response.getChildsGoods.getChildsGoods.errorCode;
                    let value = response.getChildsGoods.getChildsGoods.value;
                    if (errorCode == 0) {
                        let orderGoodss = response.getChildsGoods.getChildsGoods.orderGoodss;
                        let totalResult = response.getChildsGoods.getChildsGoods.totalResult;
                        this.orderGoodss = orderGoodss;
                        this.setState({
                            dataSource: orderGoodss,
                            totalResult: totalResult,
                            loading:false,
                        })
                    } else {
                        this.setState({
                            dataSource: [],
                            loading:false,
                        });
                        if (errorCode == 500){
                            message.error('服务器错误,请联系系统管理员');
                        }
                    }
                }

            }
        );
    }
    /**/
    handleOk(){
        let {list} = this.state;
        this.props._selectMaterial(this.props._materialIndex,list);
    }
    handleCancel = (e) => {
        this.setState({
            SelectMaterialFlag: false,
        });
    };
    /*选择面料*/
    chooseFabric(){
        this.setState({
            orderType: "3",
            loading:true,
            pageNum:"1",
        }, this.getChildsGoods);
    }
    /*选择辅料*/
    chooseRaw(){
        this.setState({
            orderType: "2",
            loading:true,
            pageNum:"1",
        }, this.getChildsGoods);
    }
    /*页码*/
    onSure(size,num){
        this.num = num;
        this.setState({
            loading:true,
            pageSize:size,
            pageNum:num,
        },this.proDigestList)
    }
    render(){
        let { dataSource } = this.state;
        const columns = [
            {
                title: '品名',
                dataIndex: 'productName',
                key: 'productName',
                width: 300,
                render: (text, record, index) => {
                    return <span title={record.productName}>{record.productName}</span>
                },
            },
            {
                title: '规格',
                dataIndex: 'specification',
                key: 'specification',
                width: 200,
                render:(text, record, index) =>{
                    return <span className="">{record.specification}</span>
                }
            },
            {
                title: '颜色',
                dataIndex: 'colour',
                key: 'colour',
                width: 200,
                render:(text, record, index) =>{
                    return <span className="">{record.colour}</span>
                }
            },{
                title: '单位',
                dataIndex: 'prickle',
                key: 'prickle',
                width: 200,
                render:(text, record, index) =>{
                    return <span className="">{record.prickle}</span>
                }
            },
        ];

// rowSelection object indicates the need for row selection
        const rowSelection = {
            type:"radio",
            onChange: (selectedRowKeys, selectedRows) => {
                // console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
                let listSelf = selectedRows.filter(()=>{
                    return
                });
                this.setState({
                    list:selectedRows,
                })
            },
            getCheckboxProps: record => ({
                // disabled: record.name === 'Disabled User',    // Column configuration not to be checked
            }),
        };
        return (
            <Modal title="选择物料"
                   visible={this.state.SelectMaterialFlag}
                   style={{ width:'100%',top : '20%' }}
                   wrapClassName="production_fabricRaw_selectMaterialModal"
                   onOk={this.handleOk.bind(this)}
                   onCancel={this.handleCancel.bind(this)}
                   okText="确定"
                   cancelText="取消"
                   maskClosable={false}
            >
                <span className={this.state.orderType=="3"? "production_fabricRaw_modal_span_active" :"production_fabricRaw_modal_span"} onClick={this.chooseFabric.bind(this)}>面料</span>
                <span className={this.state.orderType=="2"? "production_fabricRaw_modal_span_active" :"production_fabricRaw_modal_span"} onClick={this.chooseRaw.bind(this)}>辅料</span>
                <div>
                    <Table
                        rowSelection={rowSelection}
                        columns={columns}
                        dataSource={dataSource}
                        style={{width:'800px',marginTop:"20px"}}
                        className="order_table production_management_modal"
                        pagination={false}
                        rowKey={record => record.orderId+record.gid}
                        scroll={{ y: 240 }}
                        loading={this.state.loading}
                    />
                </div>
            </Modal>
        )
    }
}
